package xcmg.device.web.buyBack;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.buyBack.BackOutboundService;
import xcmg.device.service.vo.buyBack.BackInboundAddVO;
import xcmg.device.service.vo.buyBack.BackInboundVO;
import xcmg.device.service.vo.buyBack.BackOutboundVO;
import xcmg.device.service.vo.delivery.ManualFinishVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.Arrays;

/**
 * Created by wrg on 2019/4/9.
 */
@RestController
@RequestMapping("/backOutbound")
public class BackOutboundController  extends BasicController {
    @Autowired
    private BackOutboundService backOutboundService;

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<BackOutboundVO> condition)throws Exception{
        BackOutboundVO buyBackVO = condition.getFilters();
        buyBackVO.setOrgId(getOrgId());
        condition.setFilters(buyBackVO);
        PageCommonVO pageCommonVO = backOutboundService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult add(@RequestBody BackInboundAddVO backInboundAddVO)throws Exception{
        if(backInboundAddVO.getHeaderInfo() == null || backInboundAddVO.getMaterialList().isEmpty()){
            return actionResult(ErrorCode.IllegalArument);
        }

        BackInboundVO buyBackVO = backInboundAddVO.getHeaderInfo();
        buyBackVO.setCreateId(getUserId());
        buyBackVO.setOrgId(getOrgId());
        buyBackVO.setCreateName(getUserName());
        backInboundAddVO.setHeaderInfo(buyBackVO);
        try {
            return actionResult(backOutboundService.add(backInboundAddVO, getUserId(), getOrgId()));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/item",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult item(String id){
        return actionValueResult(backOutboundService.item(id));
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult update(@RequestBody BackInboundAddVO backInboundAddVO)throws Exception{
        try {
            return actionResult(backOutboundService.update(backInboundAddVO));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult remove(@RequestBody String[] ids)throws Exception{
        if(ids==null||ids.length==0){
            return actionResult(ErrorCode.IllegalArument);
        }
        try {
            return actionResult(backOutboundService.remove(Arrays.asList(ids), getOrgId()));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/export",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public void export( String[] ids)throws Exception {
        backOutboundService.export(Arrays.asList(ids));
    }

    @RequestMapping(value = "/exportAll",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public void exportAll(BackOutboundVO condition)throws Exception {
        condition.setOrgId(getOrgId());
        backOutboundService.exportAll(condition);
    }

    @RequestMapping(value = "/manualFinish", method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult manualFinish(@RequestBody ManualFinishVO manualFinishVO) throws Exception {
        try {
            return actionResult(backOutboundService.manualFinish(manualFinishVO.getBillNo(), manualFinishVO.getMaterialsNo(), getOrgId()));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }
}
